package cn.fws.meap.community.web;

import java.util.List;

import cn.fws.framework.utils.StringUtils;
import cn.fws.meap.community.enums.ArticleTypeEnum;
import cn.fws.meap.community.vo.ArticleListVo;
import cn.fws.meap.community.vo.Details;
import cn.fws.meap.user.entity.Browse;
import cn.fws.meap.user.service.IBrowseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.fws.meap.community.entity.Article;
import cn.fws.meap.community.service.IArticleService;
import cn.fws.framework.web.contoller.BaseController;
import cn.fws.framework.web.page.PagingResult;
import cn.fws.framework.web.contoller.JsonResponse;

/**
 * 文章 信息操作处理
 *
 * @author chenkh
 * @date 2019-03-07
 */
@RestController
@RequestMapping("/meap/article")
@Api(tags = "文章 信息操作处理")
public class ArticleController extends BaseController {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IBrowseService browseService;


    /**
     * 查询文章列表
     */
    //@RequiresPermissions("meap:article:list")
    @ApiOperation(value = "查询文章列表")
    @PostMapping("/list")
    public PagingResult<ArticleListVo> list(String hotArticle) {
        startPage();
        List<ArticleListVo> list = articleService.selectList(hotArticle);
        return getPagingResult(list);
    }

    /**
     * 查询最新的三条文章列表
     */
    //@RequiresPermissions("meap:article:list")
    @ApiOperation(value = "查询最新的三条文章列表")
    @PostMapping("/listNew")
    public List<Details<Article>> listNew() {
        List<Details<Article>> list = articleService.selectListNew();

        return list;
    }

    /**
     * 新增保存文章
     */
    //@RequiresPermissions("meap:article:add")
    @ApiOperation(value = "新增保存文章")
    @ApiImplicitParam(name = "article", value = "新增保存文章", dataType = "Article")
    @PostMapping("/add")
    public JsonResponse addSave(Article article) {

        //判断三个，内容，封面，标题
        if (StringUtils.isEmpty(article.getArticleTitle())) {

            return toAjax(0,"404", "标题不能为空");
        }else if(StringUtils.isEmpty(article.getTitleImg())){
            return toAjax(0,"404", "封面不能为空");
        }else if(StringUtils.isEmpty(article.getArticleMsg())){
            return toAjax(0,"404", "内容不能为空");
        }else if(article.getArticleTitle().length()>100){
            return toAjax(0,"404", "标题的字数不能大于100");
        }
        String articleId = articleService.insert(article);

        return JsonResponse.success("操作成功",articleId);

    }

    /**
     * 修改保存文章
     */
    //@RequiresPermissions("meap:article:edit")
    @ApiOperation(value = "修改保存文章")
    @ApiImplicitParam(name = "article", value = "修改保存文章", dataType = "Article")
    @PutMapping("/edit")
    public JsonResponse editSave(Article article) {
        if (StringUtils.isEmpty(article.getArticleTitle())) {
            return toAjax(0, "404", "标题不能为空");
        }
//		在文章保存之前首先将文章内的资源保存
        if (StringUtils.isNotEmpty(article.getArticleMsg()) ||
                StringUtils.isNotEmpty(article.getArticleImg()) ||
                StringUtils.isNotEmpty(article.getVideoId()) ||
                StringUtils.isNotEmpty(article.getVoiceId())) {
            return toAjax(articleService.update(article));
        }

        return toAjax(0, "404", "内容不能为空");

    }

    /**
     * 删除文章
     */
    //@RequiresPermissions("meap:article:remove")
    @ApiOperation(value = "删除文章")
    @ApiImplicitParam(name = "ids", value = "根据id删除文章", dataType = "String")
    @DeleteMapping("/remove")
    public JsonResponse remove(@RequestBody String ids) {
        return toAjax(articleService.deleteByIds(ids));
    }

    /** 文章详情 */
    @ApiOperation(value = "查询文章详情")
    @PostMapping("/getArticleDetails")
    public JsonResponse getArticleDetails(String articleId,String openId){
        int count=0;
        ArticleListVo articleDetail = articleService.getArticleDetails(articleId, openId);

        //查询当前用户有没有浏览过这篇文章
        if(!browseService.selectByArticleIdAndOpenid(articleId,openId)){
            //有这篇文章添加浏览记录
            count= browseService.insert(articleId,ArticleTypeEnum.ARTICLE.getCode(),articleDetail.getOpenid(),openId);
            System.out.println("浏览记录："+count);
        }

        return JsonResponse.success(""+count,articleDetail);

    }





}
